#include <stdio.h>
#include <string.h>

#include "gs.h"
#include "dacq.h"
#include "dgl.h"
#include "vr.h"

static DGL_DATA dgl_data;

int gs_dacq_wait(void)
{
    int ready = 0;

    ready = gs_dgl_wait();

    return ready;
}

int save_sensor_data(DGL_DATA *dgl)
{
	FILE *fp;
	time_t now_t;
	struct tm* curr;
	char filename[32];

	curr = localtime(&now_t);
	
	sprintf(filename, "./data/0000_data_%4d%02d%02d", 
			curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday);

	if ( (fp = fopen(filename, "a+")) == NULL) {
		perror ("save_sensor_data");
		return -1;
	}

	fprintf(fp, 
			"%x\t"
			"%4d%02d%02d\t"
			"%02d%02d%02d\t"
			"%.2f\t"
			"%.2f\t"
			"%.2f\n",
			dgl->addr,
			curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday,
			curr->tm_hour, curr->tm_min, curr->tm_sec,
			dgl->oil,
			dgl->water,
			dgl->temp);

	fclose (fp);

	return 0;
}

int gs_dacq_data(void)
{
    int ret;
    GS_TANK *tank, old;

    ret = gs_dgl_data(&dgl_data);
    
    tank = gs_tank_by_addr(dgl_data.addr);

    printf ("$$$$$$$$$$$$$$$$$$$$$current tank address is %x\n", dgl_data.addr);
    if (ret == 0)
    {
        printf ("++++++++++++++++++++++++++++\n");
#if 0
        printf("addr = %x\n", dgl_data.addr);
        printf("oil = %f\n", dgl_data.oil);
        printf("water = %f\n", dgl_data.water);
        printf("temp = %f\n", dgl_data.temp);
#endif
       
		printf ("dgl oil = %.0f\n", dgl_data.oil);
		printf ("tank oil = %.0f\n", tank->oil);

		if ((long) dgl_data.oil != (long) tank->oil) {
			save_sensor_data(&dgl_data);
		}

        if (tank == NULL) 
        {
            fprintf (stderr, "system is not addr: %x\n", dgl_data.addr);
            return -1;
        }

		tank->oil = dgl_data.oil;
		tank->water = dgl_data.water;
		tank->t1 = tank->t2 = tank->t3 = tank->t4 = tank->t5 = dgl_data.temp;
		tank->temp = dgl_data.temp;

#if 0
        if (gs_tank_is_wrong(tank)) {
		   if (g_gs.wrong_callback)
            	g_gs.wrong_callback(tank, NULL);
		   return -1;
		}
		else {
        	// memcpy(&old, tank, sizeof(GS_TANK));
			tank->oil = dgl_data.oil;
			tank->water = dgl_data.water;
			tank->t1 = tank->t2 = tank->t3 = tank->t4 = tank->t5 = dgl_data.temp;
			tank->temp = dgl_data.temp;
		}

        if ((gs_tank_is_alarm(tank) != gs_tank_is_alarm(&old)) && 
                g_gs.alarm_callback)
            g_gs.alarm_callback(tank, NULL);
#endif       
        if (gs_data_callback != NULL) {
            gs_data_callback(tank, NULL);
		}
#if 0
        if ((gs_leak_data(tank) != gs_leak_data(&old)) && 
                g_gs.leak_callback)
            g_gs.leak_callback(tank, NULL);
#endif
    }
    else {
        printf ("-----------------------------------\n"); 
		tank->oil = -1;
		tank->water = -1;
		tank->t1 = tank->t2 = tank->t3 = tank->t4 = tank->t5 = -1;
		tank->temp = -1;// dgl_data.temp;
        if (gs_data_callback != NULL) {
            gs_data_callback(tank, NULL);
		}
    }

    return ret;
}

int gs_dacq_init(void)
{
    // TODO
    int ret;

    ret = gs_dgl_init("/dev/ttyS1");

    return ret;
}

